// $Id: CreateBootlegCommand.java 240 2007-02-28 12:28:55Z huijzer $

/*    
	Aoide is a web-based music catalog using the Spring Framework.
	Copyright (C) 2006  Arjan Huijzer

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    
    You can contact Arjan Huijzer by sending an e-mail to huijzer@gmail.com.
*/

package org.aoide.shell.command;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.aoide.core.persistence.Artist;
import org.aoide.core.persistence.ArtistDao;
import org.aoide.core.persistence.Disc;
import org.aoide.core.persistence.DiscDao;
import org.aoide.core.persistence.Type;
import org.aoide.core.persistence.TypeDao;
import org.aoide.shell.ShellException;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;

public class CreateBootlegCommand extends BaseCommand {

	public boolean execute(ApplicationContext context) throws ShellException {

		String artist = readInput("Artist? ");
		
		ArtistDao artistDao = (ArtistDao) context.getBean("artistDao");
		DiscDao discDao = (DiscDao) context.getBean("discDao");
		TypeDao typeDao = (TypeDao) context.getBean("typeDao");

		// Check if artist is in the database
		List<Artist> artistList = artistDao.findByName(artist);
		if (artistList.size() == 0 ) {
			throw new ShellException("Artist '" + artist + "' not found.");
		} else if (artistList.size() > 1 ) {
			throw new ShellException("More than 1 artist with name '" + artist + "' found.");
		}
	
		// Get type Bootleg
		String description = "Bootleg";
		List<Type> typeList = typeDao.findByDescription(description);
		if (typeList.size() == 0 ) {
			throw new ShellException("Type '" + description + "' not found.");
		} else if (typeList.size() > 1 ) {
			throw new ShellException("More than 1 type with description '" + description + "' found.");
		}
		
		String title = readInput("Title? ");
		
		String year = readInput("Year? ");
		

		
		Disc disc = new Disc();
		disc.setId("urn:uuid:" + UUID.randomUUID().toString());
		disc.setCreated(new Date());
		disc.setTitle(title);
		disc.setArtist(artistList.get(0));
		disc.addType(typeList.get(0));
		if (StringUtils.isNotBlank(year)) {
			disc.setYear(new Integer(year));
		}
		
		discDao.save(disc);
		
		return true;
	}

}
